package com.example.geange.demo01;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.MenuView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnTouchListener{

    public static int Red = Color.parseColor("#FF5050");
    public static int Green = Color.parseColor("#13da7a");

    String js = "http://v.juhe.cn/toutiao/index?type=top&key=30a23fbe655c778e0b5ef815bf8cdb4b";

    public static boolean loadpic = true;
    public static int webBackground = Color.WHITE;
//    #EBFAC7



    private RadioGroup group;
    public static Toolbar toolbar;
    private Fragment[] objects = new Fragment[4];
    private int page = 1;
    public static ProgressBar progressBar;
    private EditText editText;

    News news_fragment = new News();
    Weather weather_fragment = new Weather();
    Address_list address_list_fragment = new Address_list();
    Setting setting_fragment = new Setting();

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    private String citycode;
    private String path = "http://wthrcdn.etouch.cn/weather_mini?citykey=";

    Context context = MainActivity.this;

    private MyDBOpenhelper dbOpenhelper = new MyDBOpenhelper(MainActivity.this, "wether.db", null, 1);



    private LinearLayout linearLayout;
    private ProgressBar freshbar;



    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {

        } else {
            NetworkInfo[] info = cm.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * Activity的启动事件
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        if (isNetworkAvailable(MainActivity.this)){
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    new MyAsycntask2().execute(js);
                }
            }.start();
        }

//        new Thread(){
//            @Override
//            public void run() {
//                super.run();
////                ConnectivityManager conm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
////                Log.i("网络连接", "conm.getActiveNetworkInfo().isAvailable()="+conm.getActiveNetworkInfo().isAvailable());
//                if (isNetworkAvailable(MainActivity.this)){
//                    new Thread(){
//                        @Override
//                        public void run() {
//                            super.run();
//                            new MyAsycntask2().execute(js);
//                        }
//                    }.start();
//                }
//            }
//        }.start();


        //实现广播的核心代码
//        myReceiver = new MyReceiver();
//        IntentFilter itFiler = new IntentFilter();
//        itFiler.addAction("android.net.conn.CONNECTIVITY_CHANGE");
//        registerReceiver(myReceiver, itFiler);

        linearLayout = (LinearLayout) findViewById(R.id.freshbar);


        group = (RadioGroup) findViewById(R.id.radiogroup);
        group.setOnCheckedChangeListener(this);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        fragmentTransaction.replace(R.id.frame, weather_fragment);
        fragmentTransaction.commit();
        toolbar.setTitle("天气预报");





        setSupportActionBar(toolbar);
//        toolbar.setBackgroundColor();
        /**
         * toolbar的事件监听
         */
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.titlebar_add: {
                        if (page == 1) {

                            Log.i("点击添加", "R.id.titlebar_add");
                            LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
                            View setcity = layoutInflater.inflate(R.layout.setcity, null);
                            editText = (EditText) setcity.findViewById(R.id.input_citycode);
                            new AlertDialog.Builder(MainActivity.this)
                                    .setTitle(R.string.set_weather)
                                    .setView(setcity)
                                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            String cityname;
                                            //这里定义对话框按钮单击之后要运行的事件
                                            cityname = editText.getText().toString();
                                            citycode = getCityCode(editText.getText().toString());
                                            Log.i("set_citycode", " "+getCityCode(editText.getText().toString()));
                                            if (!find(citycode) && citycode != null) {
                                                new MyAsyncTask().execute(citycode);
                                            }else {
                                                if (citycode == null){
                                                    Toast.makeText(MainActivity.this, "抱歉,找不到"+cityname+"地区的天气资料", Toast.LENGTH_SHORT).show();
                                                }
                                                if (find(citycode)){
                                                    Toast.makeText(MainActivity.this, cityname+"已经存在,不允许再添加", Toast.LENGTH_SHORT).show();
                                                }
                                            }

                                        }
                                    }).show();
//  java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                        }
                        break;
                    }
                    case R.id.refresh: {
                        if (page == 1) {
                            if (isNetworkAvailable(MainActivity.this)){
                                updatedataFromInternet();
                            }else {
                                Toast.makeText(MainActivity.this, "没有网络,无法刷新", Toast.LENGTH_SHORT).show();
                            }

                        }
                        if (page == 0) {
                            if (News.listNews.isEmpty()){
                                if (isNetworkAvailable(MainActivity.this)){
                                    new MyAsycntask2().execute(js);
//                                    FragmentManager fragmentManager = getFragmentManager();
//                                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//                                    fragmentTransaction.replace(R.id.frame, new Fragment());
//                                    fragmentTransaction.commit();

                                }else {
                                    Toast.makeText(MainActivity.this, "没有网络,无法刷新", Toast.LENGTH_SHORT).show();
                                }
                            }else {

                            }
//                            if (!News.listNews.isEmpty()){
//                                FragmentManager fragmentManager1 = getFragmentManager();
//                                FragmentTransaction fragmentTransaction1 = fragmentManager.beginTransaction();
//                                fragmentTransaction1.replace(R.id.frame, news_fragment);
//                                fragmentTransaction1.commit();
//                            }
                        }
                        break;
                    }
                }
                return true;
            }
        });
    }

    /**
     * 设置toolbar
     *
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    /**
     * 监听Radiobutton点击事件
     * @param radioGroup
     * @param i
     */
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        switch (i) {
            case R.id.button1: {
                page = 0;
                toolbar.setTitle("每日热讯");
                toolbar.setBackgroundColor(Red);
//                new MyAsycntask2().execute(js);
                fragmentTransaction.replace(R.id.frame, news_fragment);
                fragmentTransaction.commit();
                Log.i("关闭图片","MainActivity.loadpic = "+MainActivity.loadpic+"page="+page);
                break;
            }
            case R.id.button2: {
                page = 1;
                toolbar.setTitle("天气预报");
                toolbar.setBackgroundColor(Green);
                fragmentTransaction.replace(R.id.frame, weather_fragment);
                fragmentTransaction.commit();
                Log.i("关闭图片","MainActivity.loadpic = "+MainActivity.loadpic+"page="+page);
                break;
            }
            case R.id.button3: {
                page = 2;
                toolbar.setTitle("通讯录");
                toolbar.setBackgroundColor(Green);
                fragmentTransaction.replace(R.id.frame, address_list_fragment);
                fragmentTransaction.commit();
                break;
            }
            case R.id.button4: {
                page = 3;
                toolbar.setTitle("设置");
                toolbar.setBackgroundColor(Green);
                fragmentTransaction.replace(R.id.frame, setting_fragment);
                fragmentTransaction.commit();
                break;
            }
        }
    }

    /**
     * ontouchlistener
     * @param view
     * @param motionEvent
     * @return
     */
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }


    int startX;
    int startY;
    int currentX;
    int currentY;
    int disX;
    int disY;
    Boolean touch = true;

    /**
     * 下拉刷新
     * @param event
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        if (page == 0){
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    touch = false;
                    startX = (int) event.getRawX();
                    startY = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (!touch){
                        currentX = (int) event.getRawX();// 获取当前x坐标
                        currentY = (int) event.getRawY();// 获取当前y坐标
                        disX = currentX - startX;// x方向移动的距离
                        disY = currentY - startY;// y方向移动的距离
                        if (disX > 300){
                            FragmentManager fragmentManager = getFragmentManager();
                            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                            fragmentTransaction.replace(R.id.frame, news_fragment);
                            fragmentTransaction.commit();
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
        }
        if (page == 1){
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    touch = false;
                    startX = (int) event.getRawX();
                    startY = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (!touch){
                        currentX = (int) event.getRawX();// 获取当前x坐标
                        currentY = (int) event.getRawY();// 获取当前y坐标
                        disX = currentX - startX;// x方向移动的距离
                        disY = currentY - startY;// y方向移动的距离
                        if (Math.abs(disY) > 300){
                            updatedataFromInternet();
                            touch = true;
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
        }

        return super.dispatchTouchEvent(event);
    }

    /**
     * 解析天气的json数据,返回一个map, 第一个参数是json数据(string类型), 第二个是布尔标志,true的话插入数据,否则不插入
     * @param json
     * @return
     * @throws JSONException
     */
    public Map<String, Object> parseDiffJson(String json, boolean flag) throws JSONException {
        Map<String, Object> map = new HashMap<String, Object>();
        String hightemp;
        String lowtemp;
        String city;
        String type;
        JSONObject jsonObject0 = new JSONObject(json);
        JSONObject jsonObject1 = jsonObject0.getJSONObject("data");

        //获取城市名
        city = jsonObject1.getString("city");
        String citycode = getCityCode(city);

        JSONArray jsonArray = jsonObject1.getJSONArray("forecast");
        JSONObject jsonObject2 = jsonArray.getJSONObject(0);
        type = jsonObject2.getString("type");
        hightemp = jsonObject2.getString("high");
        lowtemp = jsonObject2.getString("low");

        map.put("address", city);
        map.put("temperature", lowtemp + " ~ " + hightemp);
        map.put("weather", type);
        map.put("pic", StartActivity.setPicture(type));

        CityWeather cityWeather = new CityWeather(citycode, city, type, lowtemp + " ~ " + hightemp);
        if (flag){
            save(cityWeather);
        }
        return map;
    }

    //获取新闻列表内容
    public List<NewsItem> parseJsonNews(String json){
        List<NewsItem> newsItems = new ArrayList<>();
        String picurl;
        String title;
        String time;
        String content = null;
        try {
            JSONObject rootjson = new JSONObject(json);
            JSONObject bodyjson = rootjson.getJSONObject("result");
            JSONArray jsonArray = bodyjson.getJSONArray("data");
            Log.i("xys", "jsonArray.length() = "+jsonArray.length());

            for (int i = 0; i < jsonArray.length(); i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                title = jsonObject.getString("title");
                time = jsonObject.getString("date");
                content = jsonObject.getString("url");
                picurl = jsonObject.getString("thumbnail_pic_s");
//                News.newsItems.add(new NewsItem(picurl, title, time, content));
                newsItems.add(new NewsItem(picurl, title, time, content));
                Log.i("qwer", title+" "+time+" "+content+" "+picurl);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return newsItems;
    }




    /**
     * 数据库操作函数, weather的
     * sqLiteDatabase.execSQL("CREATE TABLE weather(num INTEGER PRIMARY KEY AUTOINCREMENT, citycode VARCHAR(12),
     * weatherType VARCHAR(20), temperature VARCHAR(20), freshflag  INTEGER) ");
     */
    //插入数据
    public void save(CityWeather cityWeather) {
        Log.i("插入数据", "城市数字代号:"+cityWeather.cityCodenum);
        SQLiteDatabase db = dbOpenhelper.getWritableDatabase();
        db.execSQL("INSERT INTO weather(citycodenum, citycode, weatherType, temperature, freshflag) values(?,?,?,?,?)",
                new String[]{cityWeather.cityCodenum, cityWeather.cityCode, cityWeather.wetherType, cityWeather.temperature, "0"});
    }

    //更新数据
    public void update(CityWeather cw) {
        SQLiteDatabase db = dbOpenhelper.getWritableDatabase();
        db.execSQL("UPDATE weather SET weatherType = ?, temperature =? ,freshflag =? WHERE citycodenum = ? ",
                new String[]{cw.wetherType, cw.temperature, "1", cw.cityCodenum});
    }

    //获取数据库的数据的数量
    public int getCount() {
        SQLiteDatabase db = dbOpenhelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM weather", null);
        int result = cursor.getCount();
        cursor.close();
        return result;
    }

    //在sqlite中查找数据
    public Boolean find(String citycode){
        SQLiteDatabase db = dbOpenhelper.getReadableDatabase();
        Cursor cursor;
        if (citycode != null){
            cursor = db.rawQuery("SELECT * FROM weather WHERE citycodenum = ?", new String[]{citycode});
            if (cursor.moveToFirst()){
                return true;
            }
        }
        return false;
    }
//    /**
//     * =======================================================================================================
//     * @param citycodenum
//     * @return
//     */
//    //通过城市数字代码获取
//    public CityWeather getDBItem(String citycodenum){
//        SQLiteDatabase db = dbOpenhelper.getReadableDatabase();
////        Cursor cursor = db.rawQuery("SELECT * FROM weather WHERE citycodenum = ?", new String[]{citycodenum});
//        Cursor cursor = db.rawQuery("SELECT * FROM weather", null);
//        if (cursor.moveToFirst()){
//            String citycdnum = cursor.getString(cursor.getColumnIndex("citycodenum"));
//            String citycd = cursor.getString(cursor.getColumnIndex("citycode"));
//            String weatherTp = cursor.getString(cursor.getColumnIndex("weatherType"));
//            String temp = cursor.getString(cursor.getColumnIndex("temperature"));
//            Log.i("通过城市数字代码获取", citycdnum+" "+citycd+" "+weatherTp+" "+temp);
//            return new CityWeather(citycdnum, citycd, weatherTp, temp);
//        }
//        cursor.close();
//        return null;
//    }

    //获取SQLite里面的全部数据
    public List<CityWeather> getAllfromDB() {
        List<CityWeather> weathers = new ArrayList<>();
        CityWeather ctweather;
        SQLiteDatabase db = dbOpenhelper.getReadableDatabase();
        Cursor cursor = db.query("weather", null, null, null, null, null,null);
        while (cursor.moveToNext()){
            Log.i("dbxys", "执行遍历");
            String citycdnum = cursor.getString(cursor.getColumnIndex("citycodenum"));
            String citycd = cursor.getString(cursor.getColumnIndex("citycode"));
            String weatherTp = cursor.getString(cursor.getColumnIndex("weatherType"));
            String temp = cursor.getString(cursor.getColumnIndex("temperature"));
            Log.i("遍历SQLite",citycdnum+" "+citycd+" "+weatherTp+" "+temp);
            int flag = cursor.getInt(cursor.getColumnIndex("freshflag"));
            ctweather = new CityWeather(citycdnum, citycd, weatherTp, temp);
            weathers.add(ctweather);
            Log.i("遍历SQLite", "队列的个数 "+weathers.size());
        }
        cursor.close();
        return weathers;
    }

    //更新SQLite的数据
    public void updatedataFromInternet() {
        if (getCount() != 0){
            UpdateAsyncTask mytk;
            mytk = new UpdateAsyncTask();
            mytk.execute();
        }
    }


    //添加listview里面的列表
    class MyAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBar.setVisibility(View.VISIBLE);
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                Weather.list.add(parseDiffJson(s, true));
                Weather.simpleAdapter.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            progressBar.setVisibility(View.GONE);
        }
        @Override
        protected String doInBackground(String... strings) {
            return getJSON(path+strings[0]);
        }
    }

    class MyAsycntask2 extends AsyncTask<String, Void, Void>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
//            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected Void doInBackground(String... strings) {
            News.listNews = parseJsonNews(getJSONnew(strings[0]));
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
//            progressBar.setVisibility(View.GONE);
        }
    }

    class MyAsycnTaskBack extends AsyncTask<Void, Void, Void >{

        @Override
        protected Void doInBackground(Void... voids) {
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
//            fragmentTransaction.add(R.id.frame, news_fragment);

        }
    }


    //更新数据库的内部类
    class UpdateAsyncTask extends AsyncTask<Void, Void, Boolean>{
        Map<String, Object> maptmp;
        String citycodenumtmp;  //城市地址数字代码
        String citytmp;     //中文城市地址
        String WeatherTypetmp;
        String temperaturetmp;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBar.setVisibility(View.VISIBLE);
        }
        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            if (aBoolean){
                Toast.makeText(MainActivity.this, "数据已经更新", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(MainActivity.this, "数据异常,未更新", Toast.LENGTH_SHORT).show();
            }
            progressBar.setVisibility(View.GONE);
        }
        @Override
        protected Boolean doInBackground(Void... voids) {
            List<CityWeather> weathers = getAllfromDB();
            for (CityWeather cw : weathers){
                try {
                    String json = getJSON(path+cw.cityCodenum);
                    maptmp =  parseDiffJson(json, false);
                    Log.i("更新数据", maptmp.toString());
                    if (maptmp != null){
                        citytmp =  getCityCode(maptmp.get("address").toString());
                        WeatherTypetmp = getCityCode(maptmp.get("weather").toString());
                        temperaturetmp = getCityCode(maptmp.get("temperature").toString());
                        citycodenumtmp = getCityCode(citytmp);
                        //更新数据
                        update(new CityWeather(citycodenumtmp, citytmp, WeatherTypetmp, temperaturetmp));
                    }else {
                        return false;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return true;

        }
    }



    /**
     * 通过String类型的url地址获取JSON数据
     */
    private String getJSON(String path){
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        try {
            URL url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            inputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(outputStream.toByteArray());
    }


    /**
     * 获取新闻api的内容
     */
    private String getJSONnew(String url){
        String result = null;
        try {
            result = readSteam(new URL(url).openStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    private String readSteam(InputStream is){
        InputStreamReader isr;
        String result = "";
        try {
            String line = "";
            isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            while ((line = br.readLine()) != null){
                result += line;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public String getCityCode(String city){
        String CityCode;
        Map<String, String> map= new HashMap<String, String>();
        String[] cities = {};
        String[] codes = {};
        map.put("北京","101010100");map.put("上海","101020100");map.put("茂名", "101282001");map.put("钦州", "101301101");map.put("衡阳", "101250401");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");map.put("", "");
        CityCode = map.get(city);
        return CityCode;
    }

//    public boolean onKeyDown(int keyCode, KeyEvent event){
//        if(keyCode==KeyEvent.KEYCODE_BACK)
//        {
//            //Toast.makeText(this, webView.getUrl(), Toast.LENGTH_SHORT).show();
//            if(webView.canGoBack())
//            {
//                webView.goBack();//返回上一页面
//                return true;
//            }
//            else
//            {
//                System.exit(0);//退出程序
//            }
//        }
//        return super.onKeyDown(keyCode, event);
//    }

////


}
